package com.um.psystem.controller.fixtureManage;

import cn.hutool.core.util.StrUtil;
import com.um.psystem.controller.BaseController;
import com.um.psystem.entity.fixtureEntity.FixtureDetail;
import com.um.psystem.entity.fixtureEntity.FixtureType;
import com.um.psystem.entity.mtEntity.AssetsDetail;
import com.um.psystem.entity.mtEntity.AssetsType;
import com.um.psystem.model.vo.DataGrid;
import com.um.psystem.model.vo.JsonResult;
import com.um.psystem.service.fixtureService.IFixtureTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: AiLen2E
 * @Date: 2022/1/20 14:51
 * @version: 1.0
 */
@Controller
@RequestMapping(value = "/fixtureManage")
public class FixtureManageController extends BaseController {

    @Autowired
    IFixtureTypeService fixtureTypeService;

    /**
     * 物资一级分类列表(分页)
     *
     * @return
     */
    @RequestMapping(value = "/list_MainGrid", method = RequestMethod.POST)
    @ResponseBody
    public DataGrid getMainGrid(@RequestParam Map map) {
        List<FixtureType> fixtureTypeList = fixtureTypeService.getFixtureTypes(map);
        return buildDataGrid(fixtureTypeList, fixtureTypeList.size());
    }

    /**
     * 物资二级分类列表(分页)
     *
     * @param typeMainId
     * @return
     */
    @RequestMapping(value = "/list_DetailGrid", method = RequestMethod.POST)
    @ResponseBody
    public DataGrid getDetailGrid(@RequestParam("typeMainId") String typeMainId, @RequestParam("typeDtlName") String typeDtlName) {
        Map<String, Object> modelMap = new HashMap();
        if (StrUtil.isNotBlank(typeMainId)) {
            modelMap.put("type_main_id", typeMainId);
        }
        if (StrUtil.isNotBlank(typeMainId)) {
            modelMap.put("type_dtl_name", typeDtlName);
        }
        List<FixtureDetail> fixtureDetailList = fixtureTypeService.getFixtureDetails(modelMap);
        return buildDataGrid(fixtureDetailList, fixtureDetailList.size());
    }

    /**
     * 添加夹具一级分类
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/add_MainGrid", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addMainGrid(@RequestParam Map map) {
        String type_main_name = map.get("type_main_name").toString();
        String dept_name = map.get("dept_name").toString();
        String dept_id = map.get("dept_id").toString();
        String main_no = map.get("main_no").toString();
        String remark = map.get("remark").toString();
        FixtureType fixtureType = new FixtureType();
        fixtureType.setDeptId(dept_id);
        fixtureType.setDeptName(dept_name);
        fixtureType.setTypeMainNo(main_no);
        fixtureType.setRemark(remark);
        fixtureType.setTypeMainName(type_main_name);
        System.out.println(type_main_name + "&&&&" + dept_name);
        return fixtureTypeService.save(fixtureType);
    }

    /**
     * 更新夹具一级分类
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/update_MainGrid", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult updateMainGrid(@RequestParam Map map) {
        String type_main_name = map.get("type_main_name").toString();
        Integer type_main_id = Integer.parseInt(map.get("type_main_id").toString());
        String dept_name = map.get("dept_name").toString();
        String dept_id = map.get("dept_id").toString();
        String main_no = map.get("main_no").toString();
        String remark = map.get("remark").toString();
        FixtureType fixtureType = new FixtureType();
        fixtureType.setDeptId(dept_id);
        fixtureType.setDeptName(dept_name);
        fixtureType.setTypeMainNo(main_no);
        fixtureType.setRemark(remark);
        fixtureType.setTypeMainName(type_main_name);
        fixtureType.setTypeMainId(type_main_id);
        return fixtureTypeService.update(fixtureType);
    }

    /**
     * 添加物资二级分类
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/add_DtlGrid", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addDtlGrid(@RequestParam Map map) {
        String type_main_name = map.get("type_main_name").toString();
        Integer type_main_id = Integer.parseInt(map.get("type_main_id").toString());
        String type_dtl_name = map.get("type_dtl_name").toString();
        String type_dtl_no = map.get("type_dtl_no").toString();

        String type_dtl_num = type_dtl_no.substring(type_dtl_no.length()-5,type_dtl_no.length()-1);
        Integer typeDtlNum = Integer.parseInt(type_dtl_num);
        System.out.println("type_dtl_num:" + type_dtl_num);
        String k3_no = map.get("k3_no").toString();
        String unit = map.get("unit").toString();
        String model = map.get("model").toString();
        String sizes = map.get("sizes").toString();
        String use_to = map.get("use_to").toString();
        String remark_dtl = map.get("remark_dtl").toString();
        String validation_flag = map.get("validation_flag").toString();
        String supplier = map.get("supplier").toString();
        String operator = map.get("operator").toString();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date_time = df.format(new Date());

        FixtureDetail fixtureDetail = new FixtureDetail();
        fixtureDetail.setModel(model);
        fixtureDetail.setK3_code(k3_no);
        fixtureDetail.setUnit(unit);
        fixtureDetail.setUse_to(use_to);
        fixtureDetail.setRemark(remark_dtl);
        fixtureDetail.setSizes(sizes);
        fixtureDetail.setTypeDtlName(type_dtl_name);
        fixtureDetail.setTypeDtlNo(type_dtl_no);
        fixtureDetail.setTypeDtlNum(typeDtlNum);
        fixtureDetail.setTypeMainId(type_main_id);
        fixtureDetail.setTypeMainName(type_main_name);
        fixtureDetail.setValidation_flag(validation_flag);
        fixtureDetail.setSupplier(supplier);
        fixtureDetail.setOperator(operator);
        fixtureDetail.setDate_time(date_time);
        return fixtureTypeService.save_fd(fixtureDetail);
    }


    /**
     * 更新物资二级分类
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/update_DtlGrid", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult updateDtlGrid(@RequestParam Map map) {
        String type_main_name = map.get("type_main_name").toString();
        Integer type_main_id = Integer.parseInt(map.get("type_main_id").toString());
        Integer type_dtl_id = Integer.parseInt(map.get("type_dtl_id").toString());
        String type_dtl_name = map.get("type_dtl_name").toString();
        String type_dtl_no = map.get("type_dtl_no").toString();
        String model = map.get("model").toString();
        String sizes = map.get("sizes").toString();
        String remark = map.get("remark_dtl").toString();
        String k3_no = map.get("k3_no").toString();
        String unit = map.get("unit").toString();
        String use_to = map.get("use_to").toString();
        String validation_flag = map.get("validation_flag").toString();
        String supplier = map.get("supplier").toString();
        String operator = map.get("operator").toString();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date_time = df.format(new Date());

        FixtureDetail fixtureDetail = new FixtureDetail();
        fixtureDetail.setModel(model);
        fixtureDetail.setRemark(remark);
        fixtureDetail.setSizes(sizes);
        fixtureDetail.setTypeDtlName(type_dtl_name);
        fixtureDetail.setTypeDtlNo(type_dtl_no);
        fixtureDetail.setTypeMainId(type_main_id);
        fixtureDetail.setTypeMainName(type_main_name);
        fixtureDetail.setTypeDtlId(type_dtl_id);
        fixtureDetail.setK3_code(k3_no);
        fixtureDetail.setUnit(unit);
        fixtureDetail.setUse_to(use_to);
        fixtureDetail.setValidation_flag(validation_flag);
        fixtureDetail.setSupplier(supplier);
        fixtureDetail.setOperator(operator);
        fixtureDetail.setDate_time(date_time);
        return fixtureTypeService.update_fd(fixtureDetail);
    }

    /**
     * 获取新的物资编码
     *
     * @return
     */
    @RequestMapping(value = "/get_typeDtlNo", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getTypeDtlNo(@RequestParam Map map) {
        String typeDtlNo_new = fixtureTypeService.getTypeDtlNo(map);
        return JsonResult.success(typeDtlNo_new);
    }

    /**
     * 删除指定分类
     */
    @RequestMapping(value = "/delType/{typeMainId}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delType(@PathVariable Integer typeMainId) {
        return fixtureTypeService.del(typeMainId);
    }

    /**
     * 删除指定物资
     */
    @RequestMapping(value = "/delDtl/{typeDtlId}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delDtl(@PathVariable Integer typeDtlId) {
        return fixtureTypeService.del_fd(typeDtlId);
    }


}
